package cn.wellwan.commons;

import cn.wellwan.entity.User;
import cn.wellwan.service.UserService;
import cn.wellwan.service.WechatService;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 小程序拦截器
 */
public class WellwanLoginInterceptor implements HandlerInterceptor {
    private BeanFactory factory = null;
    @Autowired
    private UserService userService;
    @Autowired
    private WechatService wechatService;
    @Autowired
//    private RedisUtil redisUtil;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        factory =  WebApplicationContextUtils.getRequiredWebApplicationContext( request.getServletContext());
        String code = request.getParameter("code");
//        User user = wechatService.decode(code);
        return false;
    }

    public UserService getUserService() {
       return userService = factory.getBean(UserService.class);
    }


    public WechatService getWechatService() {
        return  wechatService = factory.getBean(WechatService.class);
    }

}
